new IScroll('.home');

var arr = ['yunqian','yunzhong','chanqian'];
var Datas = null;
$.ajax({
	url:"http://localhost/yvyv/data.json",
	dataType:"json",
	type:'get',
	success:function(data){
		Datas = data
	}
})


var ls = localStorage;

if(!ls.getItem("fav")){
	ls.setItem("fav",'[]')
}

$(".fav").click(function(){

	var tagTitle = $(this).attr("title");
	var arrFav = JSON.parse(ls.getItem("fav"));


	if( $.inArray(tagTitle,arrFav)  > -1){
		alert('已经收藏过了')
	}else{
		//添加
		addFav(tagTitle)
	}

})

function addFav(tagTitle){

	var brrFav = JSON.parse(ls.getItem("fav"));

	brrFav.push( tagTitle  )


	ls.setItem('fav',JSON.stringify(brrFav))

}

$(".box").on('click','a',function(e){

	e.preventDefault(); 

	var that = $(this).attr('href');
	var id = $(this).attr('id');


	$(that).css({
		transition: 'all .3s',
		transform:'translateX(0)'
	}).siblings().css({
		transform:'translateX(100%)'
	})

	if(e.target.parentNode.nodeName=='NAV'){
		var index = $(this).index();
			$("mark").animate({
			left:index*25+"%"
		})
	}
	

	inTo($(this));
	//进入列表、文章

	if( that =='#list'){
		//进入列表页
		getId(id);
	}else if(that=='#article'){
		//进入内容页
		getContent($(this));

	}

})

function getContent(el){
	var arr = el.data("content").split("_");

	$("#article").html(
		Datas[arr[0]]['fenlei'][arr[1]].content
	)
	$('h1').text( Datas[arr[0]]['fenlei'][arr[1]].title   )

}

//确定内容和数据相匹配
function getId(id){

	if($.inArray(id,arr) > -1){

		getLoad(id);

	}

}
//进行渲染数据
function getLoad(id){

	var str = '';

	$.each(Datas[id]['fenlei'],function(index,val){

		str+='<a href="#article" data-content="'+id+'_'+index+'"><img src="img/tu/'+val.img+'" alt=""><p>'+val.title+'</p></a>'
		
	})
	$(".list-content").html(str);
	new IScroll(".list");
	
}


function inTo(that){

	var href = that.attr("href");
	if(href=='#list'){
		$(".fan").attr("href",'#home');
		$(".fan").show();
		$(".xihuan").hide();
	}else if(href=='#home'){
		$(".fan").hide();
	}else if(href=='#article'){
		$(".xihuan").attr("title",that.find('p').text())
		$(".xihuan").show();
		$(".fan").attr('href','#list');
	}

}	